using System;
using System.Collections;
using System.Data;
using CE.Database;
/// <summary>
/// Summary description for UserProfileDB
/// </summary>
/// 
namespace CE.CarInsurance
{
    public class CarModelDB
    {
        public CarModelDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public int Insert(CarModel carmodel)
        {
            MyDatabase _db = new MyDatabase();
            int _result = -1;
            string _spName = "sprocCarModelInsertUpdateSingleItem";
            
            SortedList _sPara = new SortedList();
            
            #region Parameter
            _sPara.Add("@MakeModelId", carmodel.MakeModelId);
            _sPara.Add("@CarMakeId", carmodel.CarMakeId);
            _sPara.Add("@CarModelId", carmodel.CarModelId);
            _sPara.Add("@CarCode", carmodel.CarCode);
            _sPara.Add("@Description", carmodel.Description);
            _sPara.Add("@Body", carmodel.Body);
            _sPara.Add("@CC", carmodel.CC);
            _sPara.Add("@RefCEUserId", carmodel.RefCEUserId);
            _sPara.Add("@Status", carmodel.Status);
            _sPara.Add("@Info1", carmodel.Info1);
            _sPara.Add("@Info2", carmodel.Info2);
            _sPara.Add("@Info3", carmodel.Info3);
            _sPara.Add("@Info4", carmodel.Info4);
            _sPara.Add("@Info5", carmodel.Info5);
            #endregion

            _result = Convert.ToInt32(_db.ExecuteSP(_spName, _sPara));

            return _result;
        }

        public CarModel GetCarModel(int CarMakeId, int CarModelId)
        {
            CarModel _carmodel = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocCarModelSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@CarMakeId", CarMakeId);
            _spara.Add("@CarModelId", CarModelId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _carmodel = new CarModel();
                    _carmodel = FillDataRecord(_dt, 0);
                }
            }
            return _carmodel;
        }

        public int GetNextId(int CarMakeId)
        {
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocCarModelSelectMaxItem";
            int _result = 0;

            SortedList _spara = new SortedList();
            _spara.Add("@CarMakeId", CarMakeId);

            _result = Convert.ToInt32(_db.ExecuteSP(_spName, _spara));

            return _result;
        }

        public int GetCarModelNextId(int CarMakeId)
        {
            int _result = GetNextId(CarMakeId);

            if (_result == 0)
            {
                _result = GetNextId(0);
            }

            return _result;
        }

        public bool CheckCarModelId(int CarMakeId, int CarModelId, out int CarMakeModelId)
        {
            CarModel _carmodel = new CarModel();
            bool _result = false;
            CarMakeModelId = 0;
            _carmodel = GetCarModel(CarMakeId, CarModelId);

            if (_carmodel != null)
            {
                CarMakeModelId = _carmodel.MakeModelId;
                _result = true;
            }

            return _result;
        }

        public CarModelList GetCarModelList(int CarMakeId)
        {
            CarModelList _carmodellist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocCarModelSelectItemList";

            SortedList _spara = new SortedList();
            _spara.Add("@CarMakeId", CarMakeId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _carmodellist = new CarModelList();
                    for (int i = 0; i < _dt.Rows.Count; i++)
                    {
                        CarModel _carmodel = new CarModel();
                        _carmodel = FillDataRecord(_dt, i);
                        _carmodellist.Add(_carmodel);
                    }
                }
            }
            return _carmodellist;
        }

        public CarModelList SearchCarModelList(string CarModel)
        {
            CarModelList _carmodellist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocCarModelSearchItem";

            SortedList _spara = new SortedList();
            _spara.Add("@CarModelDescription", CarModel);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _carmodellist = new CarModelList();
                    for (int i = 0; i < _dt.Rows.Count; i++)
                    {
                        CarModel _carmodel = new CarModel();
                        _carmodel = FillDataRecord(_dt, i);
                        _carmodellist.Add(_carmodel);
                    }
                }
            }
            return _carmodellist;
        }


        public bool Update()
        {
            return true;
        }

        public bool Delete()
        {
            return true;
        }

        private CarModel FillDataRecord(DataTable myDataTable, int RowId)
        {
            CarModel myCarModel = new CarModel();
            myCarModel.MakeModelId = Convert.ToInt32(myDataTable.Rows[RowId]["MakeModelId"].ToString());
            myCarModel.CarMakeId = Convert.ToInt32(myDataTable.Rows[RowId]["CarMakeId"].ToString());
            myCarModel.CarModelId = Convert.ToInt32(myDataTable.Rows[RowId]["CarModelId"].ToString());
            myCarModel.CarCode = myDataTable.Rows[RowId]["CarCode"].ToString();
            myCarModel.Description = myDataTable.Rows[RowId]["Description"].ToString();
            myCarModel.Body = myDataTable.Rows[RowId]["Body"].ToString();
            myCarModel.CC = myDataTable.Rows[RowId]["CC"].ToString();
            myCarModel.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());
            myCarModel.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            return myCarModel;
        }

        
    }
}